<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="wrap">
        <div class="page-header"> Promise 入门</div>
        <button id="btn"> 点击抽奖</button>
    </div>
</body>
</html>
<script>
    function rand(m,n){    //随机数
        return Math.ceil(Math.random() * (n-m+1)) + m-1;
    }
    const btn = document.querySelector("#btn");
    btn.addEventListener("click",function(){
        // setTimeout(() => {
        //     let n = rand(1,100);
        //     if(n<=30){
        //        console.log("跑车一辆")
        //     }else{
        //        console.log('空空如也')
        //     }
        // }, 1000);

        //Promise 来实现  
        const p = new Promise((resolve,reject)=>{
            setTimeout(() => {
                let n = rand(1,100);
                if(n<=30){
                    resolve(n)       //ok 
                }else{
                    reject(n)        //err
                }
            }, 1000);
        })
                //指定回调函数 
        p.then((dataN)=>{     
            console.log("跑车一辆"+dataN)
        },(dataN)=>{
            console.log('空空如也'+dataN)
        })
    })

</script>